//
//  LPTokenModel.swift
//  TLenPoint
//
//  Created by Docker on 2018/3/21.
//  Copyright © 2018年 Docker. All rights reserved.
//

import UIKit
//  归档和解档路径
private let SaveUserAccountPath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent("userAccount.archive")

class LPTokenModel: NSObject ,NSCoding{

    //会话令牌
    var access_token:String?
    //会话秘钥
    var secretKey:String?
    //  kvc构造函数
    init(dic: [String: Any]) {
        super.init()
        setValuesForKeys(dic)
    }
    //  MARK:   --归档和解档
    func encode(with aCoder: NSCoder) {
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(secretKey, forKey: "secretKey")
    }
    
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        secretKey = aDecoder.decodeObject(forKey: "secretKey") as? String
    }
    //  保存用户对象
    func saveUserAccount() {
        NSKeyedArchiver.archiveRootObject(self, toFile: SaveUserAccountPath)
    }
    
    //  读取用户信息
    class func loadUserAccount() -> LPTokenModel? {
        let userAccount = NSKeyedUnarchiver.unarchiveObject(withFile: SaveUserAccountPath) as? LPTokenModel
        return userAccount
    }
    
    //  防止字段不匹配
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        //  空实现
    }
}
